package com.myProject.order.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.annotations.GenericGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by xhw on 2017/08/06.
 */

@MappedSuperclass
public abstract class IdEntity {
    private static Logger logger = LoggerFactory.getLogger(IdEntity.class);

    protected String id;
    @Id
    //@GeneratedValue(strategy = GenerationType.IDENTITY)
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name="OBJ_ID",length = 40)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    private Timestamp createDate;


    private Timestamp modifyDate;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @Column(name="CREATEDATE")
    public Timestamp getCreateDate() {
        return createDate;
    }

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    @Column(name="MODIFYDATE")
    public Timestamp getModifyDate() {
        return modifyDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public void setModifyDate(Timestamp modifyDate) {
        this.modifyDate = modifyDate;
    }

    @PrePersist
    protected void prePersistBE()
    {
        this.createDate = new Timestamp(new Date().getTime());
    }

    @PreUpdate
    protected void preUpdateBE()
    {
        this.modifyDate = new Timestamp(new Date().getTime());
    }

    public static boolean isIdEntityType(Type type) {
        if (type.toString().length() > 7) {
            try {
                if ((type.getClass().isEnum()) || (type.getClass().isInterface())) {
                    return false;
                }
                return IdEntity.class.isAssignableFrom(Class.forName(type.toString().substring(6)));
            }
            catch (Exception e)
            {
                return false;
            }
        }
        return false;
    }

}
